/**
 * @Author: ycq_work@163.com
 * @DATE: 2023/7/17
 * @Description: 
*/
#include "ConLogin.h"

bool contr::ConLogin::begin(const QString& account, const QString& password)
{
    if (account.isEmpty())
    {
        return false;
    }
    if(password.isEmpty())
    {
        return false;
    }
    this->password = getPsd(account);
    qDebug() << this->password;
    qDebug() << password;

    if(this->password == password)
    {
        return true;
    }

    return false;
}
QString contr::ConLogin::getPsd(const QString& account)
{
    QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");   // 使用QODBC链接MYSQL数据库
    if(connect(db))
    {
        QSqlQuery query;
        QString sql = "select password from user where account = " + account;
        query.exec(sql);
        QString result = nullptr;

        while(query.next())
        {
            result = query.value(0).toString();
            qDebug() << result;
        }
        db.close();
        return result;
    }
    else
    {
        return nullptr;
    }
}
bool contr::ConLogin::connect(QSqlDatabase & db)
{
    db.setHostName("localhost");                // 数据库地址
    db.setDatabaseName("Qtdemo");               // 输入QODBC设置的数据库名称
    db.setUserName("account");                  // 输入 MYSQL账号
    db.setPassword("Password");                 // 输入MYSQL密码
    bool ok = db.open();
    if(ok)
    {
        qDebug() << "connect success!";
        return true;
    }
    else return false;
}